import unittest
from pyson.gen import compactFormat, prettyFormat, PysonObjectMeta, PysonObject
from pyson.components import StringField
from argo.jdom import JdomParser
from java.io import StringReader
from pyduct.validation import ValidationInfoHolder
from pyduct.mementos import MementoInfoForFieldHolder
from pyduct.lists import ListItemsHolder
from pyduct.metadata import CommonMetaDataHolder

class ExtComposite1(ValidationInfoHolder, MementoInfoForFieldHolder, CommonMetaDataHolder, ListItemsHolder):
    pass

class ExtendedField(ValidationInfoHolder, MementoInfoForFieldHolder, CommonMetaDataHolder, ListItemsHolder, StringField): pass

class FragmentBase(PysonObject):
    @staticmethod
    def canExt():
        return 1

    def initExt(self):
        pass


class TestExtensions(unittest.TestCase):
#    def initExtComposite1(self):
#        ext1 = ExtComposite1()
#        ext1.ValidationInfo.isValid = 1
#        ext1.ValidationInfo.message = "msg1"
#        ext1.MementoInfoForField.isParameter = 1
#        ext1.CommonMetaData.title = "t1"
#        ext1.CommonMetaData.description = "d1"
#        ext1.CommonMetaData.icon = "i1"
#        ext1.ListItems.data = [["a", ], ["b", ], ["c", ]]
#        ext1.ListItems.editable = 1
#        ext1.ListItems.multiChoice = 1
#        return ext1


    def testExtComposite1(self):
        field1 = ExtendedField("field1", "value1")
        field1.MementoInfoForField.isParameter = 1
        field1.ListItems.editable = 1
        field1.ListItems.multiChoice = 1
        field1.ValidationInfo.isValid = 1
        field1.CommonMetaData.title = "Pole1"
        self.assertEquals(1, field1.MementoInfoForField.isParameter)

        builder = field1.createBuilder()

#        node = builder.build()
#        output = prettyFormat(builder)
#        print output
#        self.assertEquals(builder.build(), JdomParser().parse(StringReader(output)))

    def testDynamicComposite(self):
        components = ()

        if 1:
            class C0(ValidationInfoHolder, FragmentBase):
                def initExt(self):
                    self.__super.initExt()
                    self.ValidationInfo.isValid = 1
                    self.ValidationInfo.message = "msg1"

            if C0.canExt():
                components += (C0,)

            class C1(MementoInfoForFieldHolder, FragmentBase):
                def initExt(self):
                    self.__super.initExt()
                    self.MementoInfoForField.isParameter = 1

            if C1.canExt():
                components += (C1,)

            class C2(CommonMetaDataHolder, FragmentBase):
                def initExt(self):
                    self.__super.initExt()
                    self.CommonMetaData.title = "t1"
                    self.CommonMetaData.description = "d1"
                    self.CommonMetaData.icon = "i1"

            if C2.canExt():
                components += (C2,)

            class C3(ListItemsHolder, FragmentBase):
                def initExt(self):
                    self.__super.initExt()
                    self.ListItems.data = [["a", ], ["b", ], ["c", ]]
                    self.ListItems.editable = 1
                    self.ListItems.multiChoice = 1
            if C3.canExt():
                components += (C3,)

        Composite0 = PysonObjectMeta("Composite0", components, {})
        composite0 = Composite0()
        composite0.initExt()

        self.assertEqual(1, composite0.ValidationInfo.isValid)
        self.assertEqual("msg1", composite0.ValidationInfo.message)
        self.assertEqual(1, composite0.MementoInfoForField.isParameter)
        self.assertEqual("t1", composite0.CommonMetaData.title)
        self.assertEqual("d1", composite0.CommonMetaData.description)
        self.assertEqual("i1", composite0.CommonMetaData.icon)
        self.assertEqual(1, composite0.ListItems.editable)
        self.assertEqual(1, composite0.ListItems.multiChoice)

        builder = composite0.createBuilder()
        output = prettyFormat(builder)
        #print output
        self.assertEquals(builder.build(), JdomParser().parse(StringReader(output)))

def runTests():
    suite = unittest.TestLoader().loadTestsFromTestCase(TestExtensions)
    unittest.TextTestRunner(verbosity=2).run(suite)

